﻿@using System.Data
@{
    ViewBag.Title = "收货地址";
    Layout = "~/Areas/Mall/Views/Shared/MallLayout1.cshtml";
    var provinceTable = ViewData["Province"] as DataTable;
    var addlist = ViewData["data"] as DataTable;
}

<div class="address_">
    我的收货地址
</div>
<div id="addlist">
  
</div>



<div class="add_sh" >
    <p style="color: red;">请仔细填写收货地址：</p>
    <p>选择地区：</p>
    <div>
        <select id="province">
            <option value="0">选择省份</option>
            @{
                foreach (DataRow row in provinceTable.Rows)
                {
                    <option value="@row["ProvinceID"]">@row["ProvinceName"]</option>
                }
            }
        </select>
        <select id="city">
            <option value="0">选择城市</option>
        </select>
        <select id="district">
            <option value="0">选择区县</option>
        </select>
    </div>
    <p>设为默认：</p>
    <div>
        <input type="checkbox" id="default" style="width:20px;height: 20px;"/>
    </div>
    <p>详细地址：</p>
    <div>
        <input type="text" id="add" style="width: 80%;"/>
    </div>
    <p>收货人姓名：</p>
    <div>
        <input type="text"  id="name"style="width: 50%;" />
    </div>
    <p>联系电话：</p>
    <div>
        <input type="text" id="tell" style="width: 70%;" />
    </div>
</div>
<div class="address_tj">+添加收货地址</div>

<div style="width: 100%;height: 80px;"></div>

<script>
    $(function() {
        getlist();
    });
    function getlist() {
        $.post("@Url.Action("GetAddress")",function(data) {
            var obj = JSON.parse(data);
            var str = "";
            for (var i = 0; i < obj.length; i++) {
                var item = obj[i];
                str += " <div class=\"ad_list\">"+
                "<div class=\"ad_ad\">收货地址："+item.Address+"</div>"+
                "<div class=\"ad_ad\">电话："+item.Tell+"</div>"+
                "<div class=\"ad_op\">"+
                "<label class=\"ad_mr\" default=\"" + item.Default + "\" onclick=\"isde("+item.Id+")\">" + (item.Default == "0" ? "设为默认地址" : "默认地址") + "</label><label class=\"ad_del\" onclick=\"del("+item.Id+")\">删除</label>" +
                "</div>"+
                "</div > ";
            }
            $("#addlist").html(str);
        });
    }

    $("#province").change(function() {
        var id = $(this).val();
        $.post("@Url.Action("GetCity")", { id: id }, function(data) {
            var obj = JSON.parse(data);
            var str = " <option value=\"0\">选择城市</option>";
            for (var i = 0; i < obj.length; i++) {
                str += "<option value=\"" + obj[i].CityID + "\">" + obj[i].CityName + "</option>";
            }
            $("#city").html(str);
            $("#district").html("<option value=\"0\">选择区县</option>");
        });
    });
    $("#city").change(function() {
        var id = $(this).val();
        $.post("@Url.Action("GetDistrict")", { id: id }, function(data) {
            var obj = JSON.parse(data);
            var str = " <option value=\"0\">选择区县</option>";
            for (var i = 0; i < obj.length; i++) {
                str += "<option value=\"" + obj[i].DistrictID + "\">" + obj[i].DistrictName + "</option>";
            }
            $("#district").html(str);
        });
    });

    $(".address_tj").click(function() {
        var address = "";
        if ($("#province").val() != "0") {
            address += $("#province").find("option:selected").text();
        } else {
            alert("请选择收货省份！");
            return false;
        }
        if ($("#city").val() != "0") {
            address += $("#city").find("option:selected").text();
        } else {
            alert("请选择收货城市！");
            return false;
        }
        if ($("#district").val() != "0") {
            address += $("#district").find("option:selected").text();
        } else {
            alert("请选择收货区县！");
            return false;
        }
        var de = 0;
        if ($("#default").is(":checked")) {
            de = 1;
        }
        var add = $("#add").val();
        if (add.replace(/\s/g, "").length == 0) {
            alert("请输入详细地址！");
            return false;
        }
        address += add;
        var name = $("#name").val();
        if (name.replace(/\s/g, "").length == 0) {
            alert("请输入收件人姓名！");
            return false;
        }

        var tell = $("#tell").val();
        var reg = /^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$/;
        if (!reg.test(tell)) {
            alert("电话号码输入有误！");
            return false;
        }
        $.post("@Url.Action("EditAddress")", { defalut: de, address: address, name: name, tell: tell }, function(row) {
            if (row > 0) {
                alert("添加成功！");
                location.reload();
            } else {
                alert("操作失败");
            }
        });
    });

    function isde(id) {
        $.post("@Url.Action("IsDefault")", { id: id }, function(row) {
            if (row > 0) {
                getlist();
            }
        });
    }

    function del(id) {
        if (confirm("您确定删除该地址吗？")) {
            $.post("@Url.Action("DelAddress")",{id:id},function(row) {
                if (row > 0) {
                    getlist();
                }
            });
        }
    }
</script>